package com.controller;

import com.model._do.*;
import com.model._do.login_do.LoginStatusDO;
import com.model.dto.baseDTO.ResultDTO;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserAccessService userAccessService;

    @Autowired
    private ResultService resultService;

    /**
     * 更新用户基本信息
     * @param user 新信息
     * @return 成功提示
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultDTO updateUser(@RequestBody UserDO user){
        return userService.updateUser(user)? resultService.createSuccess("update success"): resultService.createFailed("update failed");
    }

    /**
     * 向前端刷新用户信息
     * @param status 前端存储第三方customCode
     * @return userDO 用户信息
     */
    @RequestMapping(value = "/flushUser", method = RequestMethod.POST)
    public ResultDTO flushUser(@RequestBody LoginStatusDO status) {
        String customCode = status.getCustomCode();
        LoginStatusDO currentStatus = userAccessService.getByCustomCode(customCode);
        UserDO currentUser = userService.getUserByUserId(currentStatus.getOpenId());
        if(currentUser == null) {
            return resultService.createFailed("has no current user msg");
        } else {
            return resultService.createSuccess("search success", currentUser);
        }
    }

    /**
     * 根据签约所有设备，得到所有的签约请求
     * @param sign userId
     * @return doctor，sign
     */
    @RequestMapping(value = "/getUnConfirmDoctor", method = RequestMethod.POST)
    public ResultDTO getUnConfirmDoctor(@RequestBody SignUpDO sign) {
        return resultService.createSuccess("success", userService.getUnConfirmDoctor(sign));
    }
}
